package com.magic.uschat.adapter;

import java.util.List;

import com.magic.uschat.R;
import com.magic.uschat.bean.FaceText;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class EmoteAdapter extends BaseAdapter{

	private List<FaceText> datas;
	private Context context;
	private LayoutInflater mInflater;
	
	public EmoteAdapter(Context context, List<FaceText> datas) {
		this.context = context;
		this.datas = datas;
		mInflater = LayoutInflater.from(context);
	}
	
	@Override
	public int getCount() {
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder=null;
		if (convertView==null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_face_text, null);
			holder.imgView = (ImageView) convertView.findViewById(R.id.iv_face_text);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		FaceText faceText = (FaceText) getItem(position);
		String key = faceText.text.substring(1);
		Drawable drawable = ContextCompat.getDrawable(context,context.getResources().getIdentifier(key, "drawable", context.getPackageName()));
		holder.imgView.setImageDrawable(drawable);
		return convertView;
	}

	class ViewHolder{
		ImageView imgView;
	}
}
